package com.study.spring.thread;


import java.sql.Connection;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author zhang
 * @version 1.0
 * @date 2019/6/16 0016
 * @since jdk1.8
 */
public class FutureTaskStudy {

    static Map<String, Connection> connectionPool = new ConcurrentHashMap<>();

    public static void main(String[] args) throws InterruptedException, ExecutionException {
//        Connection = connectionPool.get("a");
//        if (connection != null){
////            return connection;
//        }else{
//            Callable<Connection> callable = new Callable<Connection>( ) {
//                @Override
//                public Connection call() throws Exception {
//                    return null;
//                }
//            };
//            FutureTask<Connection> newTask = new FutureTask<>(callable);
//            connection = connectionPool.putIfAbsent("",newTask.get());
//            if (connection == null){
//                newTask.run();
//            }
////            return;
//        }

        Callable<Connection> callable = new Callable<Connection>() {
            @Override
            public Connection call() throws Exception {
                System.out.println("进入生成任务！");
                Thread.sleep(5 * 100);
                return null;
            }
        };
        FutureTask<Connection> task = new FutureTask<>(callable);
        connectionPool.putIfAbsent("a", task.get());
        Thread.sleep(50000);
        task.run();
        task.run();
        System.out.println(task.get());
        System.out.println(task.get());
    }
}
